%% Random assignment of baseline & validation groups

function [hat_theta, hat_vartheta, rep_base_se2, rep_val_se2, size_base, size_val] = random_assignment(estimates, se, sample_size, seed)

    rng(seed);

    % Initialization
    [num_rows, num_cols] = size(estimates);
    hat_vartheta = zeros(num_rows, 1);  
    hat_theta = zeros(num_rows, num_cols - 1);  
    rep_val_se2 = zeros(num_rows, 1); 
    rep_base_se2 = zeros(num_rows, num_cols - 1); 
    size_val = zeros(num_rows, 1); 
    size_base = zeros(num_rows, num_cols - 1);

    for i = 1:num_rows
        % Randomly select an index for each row
        random_idx = randi(num_cols);
        
        % Extract the selected estimate and se for the current row
        hat_vartheta(i) = estimates(i, random_idx);  
        rep_val_se2(i) = se(i, random_idx)^2;
        size_val(i) = sample_size(i, random_idx);

        % Remove the selected element from the remaining estimates and se
        remain_idx = setdiff(1:num_cols, random_idx);
        hat_theta(i, :) = estimates(i, remain_idx);
        rep_base_se2(i, :) = se(i, remain_idx).^2;
        size_base(i, :) = sample_size(i, remain_idx); 

    end

end